package com.example.navigationdemo.nav.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;
import androidx.navigation.Navigation;

import com.example.navigationdemo.R;
import com.example.navigationdemo.nav.model.Destination;
import com.example.navigationdemo.nav.model.LottieBottomBar;
import com.example.navigationdemo.nav.utils.AppConfig;
import com.example.navigationdemo.nav.utils.PixUtils;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;


/**
 * @author: tuyrt7 on 2020/9/3.
 */
public class AppLottieBottomBar extends LinearLayout implements View.OnClickListener {

    private static final int[] sIcons = new int[]{
            R.drawable.icon_tab_home_n, R.drawable.icon_tab_msg_n, R.drawable.icon_tab_center,
            R.drawable.icon_tab_find_n, R.drawable.icon_tab_mine_n
    };

    public AppLottieBottomBar(Context context) {
        this(context, null);
    }

    public AppLottieBottomBar(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public AppLottieBottomBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        initView(context, attrs);
    }

    @SuppressLint("UseCompatLoadingForDrawables")
    private void initView(Context context, AttributeSet attrs) {
        setOrientation(LinearLayout.HORIZONTAL);

        LottieBottomBar config = AppConfig.getLottieBottomBarConfig();

        List<LottieBottomBar.Tab> tabs = config.tabs;
        for (int i = 0; i < tabs.size(); i++) {
            LottieBottomBar.Tab tab = tabs.get(i);

            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(context, attrs);
            layoutParams.width = LayoutParams.WRAP_CONTENT;
            layoutParams.height = LayoutParams.WRAP_CONTENT;
            layoutParams.weight = 1;
            if (TextUtils.isEmpty(tab.title)) {
                layoutParams.bottomMargin = PixUtils.dp2px(24);
                layoutParams.leftMargin = PixUtils.dp2px(3);
            } else {
                layoutParams.bottomMargin = PixUtils.dp2px(3);
            }

            LottieTabView lottieTabView = new LottieTabView(context);
            addView(lottieTabView, tab.index, layoutParams);

            lottieTabView.setId(tab.pageUrl.hashCode());
            lottieTabView.setOnClickListener(this);
            lottieTabView.setTextNormalColor(Color.parseColor(config.inActiveColor));
            lottieTabView.setTextSelectColor(Color.parseColor(config.activeColor));
            lottieTabView.setTextNormalSize(config.normalTextSize);
            lottieTabView.setTextSelectSize(config.selectTextSize);
            lottieTabView.setPageUrl(tab.pageUrl);
            lottieTabView.setAnimationPath(tab.lottieIconPath);
            lottieTabView.isLottieView(tab.isLottie);
            lottieTabView.setTitle(tab.title);
            lottieTabView.setIconNormal(context.getResources().getDrawable(sIcons[i]));
            lottieTabView.setIconSize(tab.size);

            lottieTabView.unSelected();
        }

        LottieTabView selectLottieTabView = (LottieTabView) getChildAt(config.selectTab);
        selectLottieTabView.selected();
    }

    @Override
    public void onClick(View v) {
        if (v instanceof LottieTabView) {
            hideAllChild();
            LottieTabView clickTab = (LottieTabView) v;
            clickTab.selected();

            String pageUrl = clickTab.getPageUrl();
            Destination destination = AppConfig.getDestConfig().get(pageUrl);
            listener.onSelectItem(destination.id);
        }
    }

    private void hideAllChild() {
        int childCount = getChildCount();
        for (int i = 0; i < childCount; i++) {
            LottieTabView child = (LottieTabView) getChildAt(i);
            child.unSelected();
        }
    }

    public void setSelectedItemId(int selectId) {
        HashMap<String, Destination> destConfig = AppConfig.getDestConfig();
        Iterator<Destination> iterator = destConfig.values().iterator();
        String pageUrl = "";
        while (iterator.hasNext()) {
            Destination next = iterator.next();
            if (next.id == selectId) {
                pageUrl = next.pathUrl;
            }
        }
        if (!TextUtils.isEmpty(pageUrl)) {
            int childCount = getChildCount();
            for (int i = 0; i < childCount; i++) {
                LottieTabView child = (LottieTabView) getChildAt(i);
                String childPageUrl = child.getPageUrl();
                if (TextUtils.equals(pageUrl, childPageUrl)) {
                    child.callOnClick();
                    break;
                }
            }
        }
    }

    private OnSelectItemListener listener;

    public void setOnSelectItemListener(OnSelectItemListener listener) {
        this.listener = listener;
    }

    public interface OnSelectItemListener {

        void onSelectItem(int id);
    }

}
